<?php
namespace Kanin\Games;

use Kanin\Games\lib\Config;
use Kanin\Games\lib\MongoPool;
use Kanin\Games\lib\Pool;
use Swoole\Table;
use \Swoole\WebSocket\Server;

class gameInit
{

	/**
	 * @var \Swoole\Database\RedisPool
	 */
	public static $redisPool;

	/**
	 * @var MongoPool
	 */
	public static $mongoPool;

	public static function run()
    {

		//连接池初始化
		 Pool::init();

        $gameServer = new gameServer();
        $config = Config::get('server');

		//记录用户信息
		$gameServer::$userTable = new Table(1024);
		$gameServer::$userTable->column('uid', Table::TYPE_INT);
		$gameServer::$userTable->column('fd', Table::TYPE_INT);
		$gameServer::$userTable->column('timer_id', Table::TYPE_INT);
		$gameServer::$userTable->create();

		//绑定fd和uid
		$gameServer::$fdTable = new Table(1024);
		$gameServer::$fdTable->column('fd', Table::TYPE_INT);
		$gameServer::$fdTable->column('uid', Table::TYPE_INT);
		$gameServer::$fdTable->column('timer_id', Table::TYPE_INT);
		$gameServer::$fdTable->create();

        $server = new Server($config['host'], $config['port']);
        $server->set($config['set']);

        //监听服务启动事件
        $server->on('workerStart', [$gameServer, 'onWorkerStart']);
        //监听连接进入事件
        $server->on('Open', [$gameServer, 'onOpen']);
        //监听数据接收事件
        $server->on('Message', [$gameServer, 'onMessage']);
        //监听连接关闭事件
        $server->on('Close', [$gameServer, 'onClose']);
        //处理异步任务(此回调函数在task进程中执行)
        $server->on('Task', [$gameServer, 'onTask']);
        //处理异步任务的结果(此回调函数在worker进程中执行)
        $server->on('Finish', [$gameServer, 'onFinish']);
		//进程退出事件
        $server->on('workerExit', [$gameServer, 'onWorkerExit']);

        //启动服务器
        $server->start();
    }
}
